package com.lin.stockbackend.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lin.stockbackend.common.BaseResponse;
import com.lin.stockbackend.common.ErrorCode;
import com.lin.stockbackend.common.ResultUtils;
import com.lin.stockbackend.exception.BusinessException;
import com.lin.stockbackend.model.entity.Stock;
import com.lin.stockbackend.service.StockService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/stock")
public class StockController {

    @Resource
    private StockService stockService;

    /**
     * 获取单条股票信息
     */
    @GetMapping("/get/{id}")
    public BaseResponse<Stock> getStockById(@PathVariable("id") Long stockId) {
        if (stockId == null || stockId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Stock stock = stockService.getById(stockId);
        if (stock == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(stock);
    }

    /**
     * 根据股票板块 ID 获取对应的股票列表
     */
    @GetMapping("/list/by_board/{boardId}")
    public BaseResponse<List<Stock>> getStocksByBoardId(@PathVariable("boardId") Long boardId) {
        if (boardId == null || boardId <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        List<Stock> stockList = stockService.list(new QueryWrapper<Stock>().eq("board_id", boardId));
        if (stockList.isEmpty()) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        return ResultUtils.success(stockList);
    }

}
